package event.action;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.bson.types.ObjectId;

import com.opensymphony.xwork2.ActionSupport;

import common.action.CommonAction;
import common.util.AlbumType;

import event.model.EventModel;
import event.service.EventService;


public class EventAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	private EventService eventService;
	
	

	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}

	private List<EventModel> list;
	
	
	
	
	public List<EventModel> getList() {
		return list;
	}
	public void setList(List<EventModel> list) {
		this.list = list;
	}

	private int result;
	

	public int getResult() {
		return result;
	}
	public void setResult(int result) {
		this.result = result;
	}
	
	public String selectEvent() {
		//test userId;
		HashMap param = new HashMap();
		param.put("userId", "test");
		
		list = eventService.selectEvent(param);
		
		
		System.out.println();		
		
		return SUCCESS;
		
	}
	
	public String insertEvent(){
		EventModel model = new EventModel();
		
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String name = request.getParameter("albumName");
		String userId = request.getParameter("userId");
		String startDate = request.getParameter("albumStartDate");
		String type = request.getParameter("albumType");
		 
		
		//505d2021329f341fdc6b9fe8
		model.setAlbumName(name);
		model.setUserId(new ObjectId("505d2021329f341fdc6b9fe8"));
		model.setAlbumType(AlbumType.getAlbumType(Integer.valueOf(type)));
		
		//startDate yyyy-MM-dd
		String yyyy = startDate.substring(0, 4);
		String mm = startDate.substring(5, 7);
		String dd = startDate.substring(8, 10);
		
		Calendar cal = Calendar.getInstance();
		cal.set(Integer.valueOf(yyyy), Integer.valueOf(mm), Integer.valueOf(dd));
		model.setAlbumStartDate(cal.getTime());
		System.out.println(yyyy + (Integer.valueOf(mm)+1) + dd);
		eventService.insertEvent(model);
		return SUCCESS;
	}
	
	public String updateEvent() {
		//5051f2064728c2558a70735e
		HashMap param = new HashMap();
		param.put("userId", "test");
		HttpServletRequest request = ServletActionContext.getRequest();
		String albumName = request.getParameter("albumName");
		String albumId = request.getParameter("albumId");
		param.put("albumName", albumName);
		param.put("albumId", albumId);
		eventService.updateEvent(param);
		return SUCCESS;
	}
	
	public String deleteEvent() { 
		HttpServletRequest request = ServletActionContext.getRequest();
		HashMap param = new HashMap();
		param.put("userId", "test");
		
		//HttpServletRequest request = ServletActionContext.getRequest();
		String albumId = request.getParameter("albumId");
		
		System.out.println("delete " + albumId);
		param.put("albumId", new ObjectId(albumId));
		
		eventService.deleteEvent(param);
		return SUCCESS;
	}
/**
	public List<HashMap> selectEvent(HashMap param) {
		//test userId;
		
		param.put("userId", "test");
		
		
		return eventService.selectEvent(param);
		
	}
	
	public int insertEvent(HashMap param){
		param.put("userId", "test");
		eventService.insertEvent(param);
		return 1;
	}
	
	public int updateEvent(HashMap param) { 
		param.put("userId", "test");
		eventService.updateEvent(param);
		return 1;
	}
	
	public int deleteEvent(HashMap param) { 
		param.put("userId", "test");
		eventService.deleteEvent(param);
		return 1;
	}
*/	
}
